package com.dao.Impl;

import com.dao.StorageDao;
import com.entity.PageBean;
import com.entity.Storage;
import com.util.JDBC;
import com.util.TransBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
public class StorageDaoImpl implements StorageDao {
    @Override
    public void show(PageBean pageBean) {
        String sql = "select count(*) from storage where status=1 or status=2 %s ";
        String whereStr = "";
        String[] whereArr = pageBean.getFactor();
        if (whereArr != null) {
            for (String s : whereArr) {
                whereStr += " and " + s;
            }
        }
        sql = String.format(sql, whereStr);
        List<List> lists = JDBC.selectArray(sql);
        if (lists != null) {
            Integer count = ((Long) lists.get(0).get(0)).intValue();
            pageBean.setRowCount(count);
        }

        sql = "select id,storageId,staffId,createTime,price,momo,status from storage " +
                "where status=1 or status=2 %s order by status asc limit ?,?";
        sql = String.format(sql, whereStr);
        List<Map> maps = JDBC.selectMap(sql, (pageBean.getPageNo() - 1) * pageBean.getPageSize(), pageBean.getPageSize());
        if (maps != null) {
            pageBean.setData(TransBean.populate(Storage.class, maps));

        }
    }
    public void showPage(PageBean pageBean) {
        String sql = "select count(*) from storage,staff where staff.staffId = storage.staffId %s ";
        String whereStr = "";
        String[] whereArr = pageBean.getFactor();
        if (whereArr != null) {
            for (String s : whereArr) {
                whereStr += " and " + s;
            }
        }
        sql = String.format(sql, whereStr);
        List<List> lists = JDBC.selectArray(sql);
        if (lists != null) {
            Integer count = ((Long) lists.get(0).get(0)).intValue();
            pageBean.setRowCount(count);
        }

        sql = "select storage.id,storageId,staffName,storage.staffId,createTime,price,storage.momo,storage.status from storage,staff where staff.staffId = storage.staffId %s order by status asc limit ?,?";
        sql = String.format(sql, whereStr);
        List<Map> maps = JDBC.selectMap(sql, (pageBean.getPageNo() - 1) * pageBean.getPageSize(), pageBean.getPageSize());
        if (maps != null) {
            pageBean.setData(maps);
        }
    }

    @Override
    public int updateStatus(int id,int status) {
        return JDBC.sqlUpdate("update storage set status=? where storageId=?",status,id);
    }

    @Override
    public int delete(int id) {
        return JDBC.sqlUpdate("delete from storage where id=?",id);
    }


    /**
     * 订单编号查询
     * @return
     */
    @Override
    public int findStorageId() {
        List<List> lists = JDBC.selectArray("select max(storageId) from storage ");
        return (Integer)lists.get(0).get(0);
    }

    @Override
    public Storage findByStorageId(int storageId) {
        return (Storage) TransBean.populate(Storage.class,JDBC.selectMap("select id,storageId,staffId,createTime,price,momo,status from storage where " +
                " storageId=?",storageId)).get(0);
    }

    @Override
    public int addStorage(Storage storage) {
//        return JDBC.sqlUpdate("insert into storage(storageId,goodsId,goodsPrice,goodsCount,staffId,createTime,goodsTypeId,status,momo)" +
//                " values(?,?,?,?,?,now(),?,0,?)",
//                storage.getStorageId(),
//                storage.getGoodsId(),
//                storage.getGoodsPrice(),
//                storage.getGoodsCount(),
//                storage.getStaffId(),
//                storage.getGoodsTypeId(),
//                storage.getMomo()
//                );
        return 0;
    }

    /**
     * 订单添加
     * @param storage
     * @return
     */
    @Override
    public int insertStorage(Storage storage) {
        return JDBC.sqlUpdate("insert into storage(storageId,staffId,createTime,price,status) values(?,?,now(),?,0)",
                storage.getStorageId(),
                storage.getStaffId(),
                storage.getPrice()
                );
    }

    @Override
    public List<Map> findStorageByStorageId(Integer storageId) {
        return JDBC.selectMap("select staff.staffName,storage.storageId, goods.name, storage.price totPrc,goodsPrice, goods.price, goodsCount, storage.momo from storageinfo,storage,staff,goods where storageinfo.storageId=? and storageinfo.storageId=storage.storageId and storageinfo.goodsId=goods.goodsId and staff.staffId=storage.staffId ", storageId);
    }

    @Override
    public int modifyMomo(Storage storage) {
        return JDBC.sqlUpdate("update storage set momo=?,status=3 where storageId=?", storage.getMomo(), storage.getStorageId());
    }

    @Override
    public List<Integer> findGoodsIdByStorageId(Integer storageId) {
        List<List> lists = JDBC.selectArray("select goodsId from storageInfo where storageId=?", storageId);
        if (lists.get(0) != null) {
            List<Integer> list = new ArrayList<>();
            for (List list1 : lists) {
                list.add((Integer) list1.get(0));
            }
            return list;
        }
        return null;
    }
}
